//
//  WmcDevEnvironmentCell.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/6/22.
//

import UIKit

class WmcDevEnvironmentCell: WmcBaseTableCell {
    // Internal

    override func makeUI() {
        super.makeUI()

        lineView.backgroundColor = .wmc_grayH19_F5F5F5
        titleLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)

        contentView.addSubview(lightView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(descLabel1)
        contentView.addSubview(descLabel2)
        contentView.addSubview(lineView)

        lightView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(8)
        }

        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(lightView.snp.right).offset(8)
            make.centerY.equalToSuperview()
            make.height.equalTo(20)
            make.width.equalTo(75)
        }

        descLabel1.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right).offset(12)
            make.top.equalTo(10)
            make.bottom.equalTo(contentView.snp.centerY)
            make.right.equalTo(-12)
        }

        descLabel2.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right).offset(12)
            make.top.equalTo(contentView.snp.centerY)
            make.bottom.equalTo(-10)
            make.right.equalTo(-12)
        }

        lineView.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }

    func bindModel(_ model: WmcDevEnvironmentVC.WmcDevEnvironmentModel) {
        titleLabel.text = model.name
        descLabel1.text = model.host1
        descLabel2.text = model.host2
        lightView.isHidden = !model.isSelected
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    private func alphaLight(time: CGFloat) -> CABasicAnimation {
        let animation = CABasicAnimation(keyPath: "opacity")
        animation.fromValue = 1
        animation.toValue = 0.1
        animation.autoreverses = true
        animation.duration = CFTimeInterval(time)
        animation.repeatCount = 1000
        animation.isRemovedOnCompletion = false
        animation.fillMode = .forwards
        animation.timingFunction = CAMediaTimingFunction(name: .easeIn)
        return animation
    }

    // Private

    private lazy var lightView: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_green_00FF00
        view.layer.cornerRadius = 4
        view.layer.masksToBounds = true
        view.layer.add(alphaLight(time: 3), forKey: "aAlpha")
        return view
    }()

    private lazy var descLabel1: UILabel = {
        let label = UILabel()
        label.font = 13.wmc_font
        label.textColor = .wmc_grayH5_606066
        label.textAlignment = .right
        return label
    }()

    private lazy var descLabel2: UILabel = {
        let label = UILabel()
        label.font = 13.wmc_font
        label.textColor = .wmc_grayH5_606066
        label.textAlignment = .right
        return label
    }()
}
